/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.ResourceManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.ResourceCreateForm;
import info.peigen.hotpot.manage.core.manage.form.ResourceEditForm;
import info.peigen.hotpot.manage.core.manage.form.ResourceSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.ResourceManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.ResourceEntity;
import info.peigen.hotpot.manage.core.service.ResourceService;
import info.peigen.hotpot.manage.core.service.order.ResourceCreateOrder;
import info.peigen.hotpot.manage.core.service.order.ResourceEditOrder;
import info.peigen.hotpot.manage.core.service.result.ResourceCreateResult;
import info.peigen.hotpot.manage.core.service.result.ResourceEditResult;
import info.peigen.hotpot.manage.core.service.result.info.ResourceInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(ResourceManageService)</b>
 * 管理后台 "资源" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class ResourceManageService {
    @Inject
    ResourceManageRepository resourceManageRepository;
    @Inject
    ResourceService          resourceService;

    public PageResult<ResourceInfo> page(ResourceSearchForm form) {
        LambdaQuery<ResourceEntity> lambdaQuery = query(form);
        return resourceManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, ResourceManageConvertor.INSTANCE::convertToInfo);
    }

    public List<ResourceInfo> list(ResourceSearchForm form) {
        LambdaQuery<ResourceEntity> lambdaQuery = query(form);
        List<ResourceEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(ResourceManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public ResourceInfo create(ResourceCreateForm form) {
        ResourceCreateOrder  order  = ResourceManageConvertor.INSTANCE.convertToCreateOrder(form);
        ResourceCreateResult result = resourceService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public ResourceInfo edit(ResourceEditForm form) {
        ResourceEditOrder  order  = ResourceManageConvertor.INSTANCE.convertToEditOrder(form);
        ResourceEditResult result = resourceService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        resourceManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        resourceManageRepository.removeByIds(ids);
    }

    public ResourceInfo detail(Integer id) {
        ResourceEntity entity = resourceManageRepository.getMapper().single(id);
        return ResourceManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public ResourceInfo detail(String resourceId) {
        ResourceEntity entity = resourceManageRepository.getMapper().findByResourceId(resourceId);
        return ResourceManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<ResourceEntity> query(ResourceSearchForm form) {
        return resourceManageRepository.getMapper().createLambdaQuery()
                .andLike(ResourceEntity::getResourceId, Query.filterLikeEmpty(form.getResourceId(), true, true))
                .andLike(ResourceEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}